package com.edu.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.edu.boot.common.api.CommonResult;
import com.edu.boot.config.UserContext;
import com.edu.boot.entity.RecuritType;
import com.edu.boot.entity.SResume;
import com.edu.boot.service.RecuritTypeService;
import com.edu.boot.service.SResumeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/recuritTypes")
public class RecuritTypeController {
    
    @Autowired
    private RecuritTypeService recuritTypeService;
    
    @Autowired
    SResumeService sResumeService;
    
    /**
     * 根据id查询招聘类型
     * @param value
     * @return
     */
    @GetMapping("/getRecuritTypeById")
    public CommonResult<RecuritType> getRecuritTypeById(@RequestParam("value") Long value) {
        RecuritType recuritType = recuritTypeService.getRecuritTypeById(value);
        if (recuritType != null) {
            return CommonResult.success(recuritType);
        } else {
            return CommonResult.failed();
        }
    }
    
    /**
     * 新增招聘类型
     * @param recuritType
     * @return
     */
    @PostMapping("/add")
    public CommonResult<Boolean> addRecuritType(@RequestBody RecuritType recuritType) {
        boolean saved = recuritTypeService.addRecuritType(recuritType);
        return CommonResult.success();
    }
    
    /**
     * 修改招聘类型
     * @param recuritType
     * @return
     */
    @PostMapping("/update")
    public ResponseEntity<Boolean> updateRecuritType(@RequestBody RecuritType recuritType) {
        boolean updated = recuritTypeService.updateRecuritType(recuritType);
        return ResponseEntity.ok(updated);
    }
    
    /**
     * 删除招聘类型
     * @param value
     * @return
     */
    @GetMapping("/delete")
    public CommonResult<Boolean> deleteRecuritType(@RequestParam("value") Long value) {
        boolean deleted = recuritTypeService.deleteRecuritType(value);
        return CommonResult.success();
    }
    
    /**
     *  获取所有招聘类型
     * @return
     */
    @GetMapping("/list")
    public CommonResult<List<RecuritType>> getRecuritTypeList() {
        List<RecuritType> recuritTypeList = recuritTypeService.getRecuritTypeList();
        return CommonResult.success(recuritTypeList);
    }
    
    /**
     * 获取行业列表，并填充用户技能
     * @return
     */
    @GetMapping("/userList")
    public CommonResult<List<RecuritType>> getUserRecuritTypeList() {
        // 获取所有招聘类型
        List<RecuritType> recuritTypeList = recuritTypeService.getRecuritTypeList();
        LambdaQueryWrapper<SResume> queryWrapper = new LambdaQueryWrapper<SResume>()
                .eq(SResume::getUId, UserContext.getUserId());
        // 获取用户简历
        SResume resume = sResumeService.getOne(queryWrapper);
        for(int i = 0; i < recuritTypeList.size(); i++){
            StringBuilder skill = new StringBuilder();
            if(Objects.nonNull(resume)){
                if(StringUtils.isNotBlank(resume.getJinengOne()) && resume.getJinengOne().equals("是") ){
                    skill.append("CET-4、");
                }
                if(StringUtils.isNotBlank(resume.getJinengTwo()) && resume.getJinengTwo().equals("是") ){
                    skill.append("计算机二级、");
                }
                if(StringUtils.isNotBlank(resume.getJinengThree()) && resume.getJinengThree().equals("是") ){
                    skill.append("，普通话二甲");
                }
                recuritTypeList.get(i).setSkill(skill.toString());
            }
        }
        return CommonResult.success(recuritTypeList);
    }
    /**
     * 计算个人薪资水平，在行业基础薪资的基础上，四六级加20%，计算机二级加15%，普通话加10%
     * @param value
     * @return  TODO
     */
    @GetMapping("/getBaseSalary/{value}")
    public CommonResult getBaseSalary(@PathVariable Integer value) {
    
        // 获取招聘类型
        RecuritType recuritType = recuritTypeService.getById(value);
        if (Objects.isNull(recuritType)) {
            return CommonResult.failed("当前行业类型不存在，请联系管理员核实");
        }
        // 获取行业基础薪资
        BigDecimal baseSalary = recuritType.getBaseSalary();
        // 获取用户简历
        SResume resume = sResumeService.getResumeByUId(UserContext.getUserId());
        if (Objects.isNull(resume)) {
            return CommonResult.failed("当前用户未填写简历，无法计算薪资标准");
        }
        String jinengOne = resume.getJinengOne();
        String jinengTwo = resume.getJinengTwo();
        String jinengThree = resume.getJinengThree();
        if(StringUtils.isNotBlank(jinengOne) && jinengOne.equals("是") ){
            baseSalary = baseSalary.add(baseSalary.multiply(new BigDecimal("0.20")));
        }
        if(StringUtils.isNotBlank(jinengTwo) && jinengTwo.equals("是") ){
            baseSalary = baseSalary.add(baseSalary.multiply(new BigDecimal("0.15")));
        }
        if(StringUtils.isNotBlank(jinengThree) && jinengThree.equals("是") ){
            baseSalary = baseSalary.add(baseSalary.multiply(new BigDecimal("0.10")));
        }
        return CommonResult.success(baseSalary);
    }
}
